package websql;

import java.sql.ResultSet;

public class Query {
	public static String[] getQueryNames() throws Exception {
		String sql = "select qname from query";
		ResultSet rs = DBcon.getConn().createStatement().executeQuery(sql);
		rs.last();
		String qname[] = new String[rs.getRow()];
		rs.beforeFirst();
		while (rs.next()) {
			String tmp = rs.getString("qname");
			tmp = unit.EscapeUnescape.unescape(tmp);
			qname[rs.getRow() - 1] = tmp;
		}
		return qname;
	}

	public static String[] getQueryNames(int cid) throws Exception {
		String sql = "select qname from query where cid='"+cid+"'";
		ResultSet rs = DBcon.getConn().createStatement().executeQuery(sql);
		rs.last();
		String qname[] = new String[rs.getRow()];
		rs.beforeFirst();
		while (rs.next()) {
			String tmp = rs.getString("qname");
			tmp = unit.EscapeUnescape.unescape(tmp);
			qname[rs.getRow() - 1] = tmp;
		}
		return qname;
	}

	public static int getqid(String what, String iswhat) throws Exception {
		String sql = "select qid from query where " + what + " = '" + iswhat
				+ "'";
		ResultSet rs = DBcon.getConn().createStatement().executeQuery(sql);
		rs.next();
		int qid = rs.getInt("qid");
		return qid;
	}

	public static String get(String what, int whichqid) throws Exception {
		String sql = "select " + what + " from query where qid = '" + whichqid
				+ "'";
		ResultSet rs = DBcon.getConn().createStatement().executeQuery(sql);
		rs.next();
		String t = unit.EscapeUnescape.unescape(rs.getString(what));
		return t;
	}

}
